<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

 use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Art;
use Auth;

use Captcha;
 use DB;
 class ArticleController extends Controller
 {


  //验证码方法

    public  function getCode()
    {



       /* $phraseBuilder = new PhraseBuilder(3, '23456789');
        $builder = new CaptchaBuilder();
         $builder->build(200,60);
         $content=$builder->output();
         return response($content)->header('Content-Type', 'image/jpeg');*/
      // return  Captcha::src();
        echo captcha_img();
    }

    public function getVc()
    {
       // echo Captcha::generate();
        //dd(Captcha::check('f4bj'));
    }


     /**
      * Display a listing of the resource.
      *
      * @return \Illuminate\Http\Response
      */
     public function index()
     {
         $new = DB::table('new')->orderBy('id','desc')->paginate(2);
         return view("art.index",['new'=>$new]);
      //       $arts = Art::orderBy('id','desc')->paginate(2);
      // return view('art.index',['arts'=>$arts]);

      }

     /**
      * Show the form for creating a new resource.
      *
      * @return \Illuminate\Http\Response
     */
     public function create()
     {
         return view("art.add");
     }

     /**
      * Store a newly created resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
      * @return \Illuminate\Http\Response
      */
     public function store(Request $request)
     {

        $data = $request->except('_token');
        $res = DB::table('new')->insert($data);
        if($res){
             return redirect('/art');
         }
         else{
             return back();
         }

    }

     /**
      * Display the specified resource.
      *
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function show($id)
     {
         $art = DB::table("new")->where("id","=",$id)->first();
         return view("art.show",["art"=>$art]);
     }

     /**
      * Show the form for editing the specified resource.
      *
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function edit($id)
     {
         $art = DB::table('new')->where('id','=',$id)->first();
         return view('art.edit',['art'=>$art]);

     }

     /**
      * Update the specified resource in storage.
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function update(Request $request, $id)
     {
         $res = DB::table('new')->where('id','=',$id)
            ->update(['title'=>$request->title,'content'=>$request->content]);
              return redirect('/art');
     }

     /**
      * Remove the specified resource from storage.
      *
      * @param  int  $id
      * @return \Illuminate\Http\Response
      */
     public function destroy($id)
     {
         $res = DB::table('new')->where('id',$id)->delete();
         return redirect('/art');
     }






      public  function  getUser()
    {
        dd(Auth::user());

        //dd(Auth::check());

    }
    public function  getLogout()
    {
        Auth::logout();
    }

    public  function postLogin(Request $request)
    {

        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);

       if($res){
           return redirect('/art');
       }else{
           return back();
       }
    }
 }
